package com.bjc.nio.c1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @program: nettypro
 * @description: filechannel传输数据
 * @author: bjc
 * @create: 2021-09-16 20:20
 */
public class TestFileChannelTransferTo {

    public static void main(String[] args) {
        try {
            FileChannel from = new FileInputStream("D:\\iotest_delete\\1.txt").getChannel();
            FileChannel to = new FileOutputStream("D:\\iotest_delete\\to.txt").getChannel();
            //效率高  底层会使用操作系统的零拷贝进行优化  一次最多传2g数据
            // 可以for循环改进
            long size = from.size();
            for (long left = size;left>0;){
                System.out.println("position:"+(size-left)+"left:"+left);
                left-=from.transferTo((size-left),left,to);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
